<?php

namespace handle\message\lib\manage\lib;

use handle\message\lib\manage\traits\Observer;
use handle\message\lib\socket\SocketHandler;

class Player
{
    use Observer;
    /**
     * 房间ID
     */
    private string $room_id;

    /**
     * 玩家ID
     * @var int
     */
    private int $uid;

    /**
     * 玩家名称
     * @var string
     */
    private string $name;

    /**
     * 头像
     * @var string
     */
    private string $pic;

    /**
     * 分数
     * @var int
     */
    private int $score;

    /**
     * 棋子
     * @var int
     * @params 1: 白棋, 2: 黑棋
     */
    private int $chess;

    /**
     * 开局先后顺序
     * @var int
     * @params 1: 先手, 2: 后手
     */
    private int $mover;

    /**
     * 玩家是否是房主
     * @var int
     * @params 1: 是, 2: 否
     */
    private int $homeowner;

    /**
     * 是否发送过悔棋
     * @params 1未发送，2已经发送-等待响应，3: 已经发送-已响应
     * @var int
     */
    private int $sendWithdraw = 1;

    /**
     * 是否发送过和棋
     * @params 1未发送，2已经发送-等待响应，3: 已经发送-已响应
     * @var int
     */
    private int $sendGameTie = 1;

    /**
     * 是否发送过认输
     * @params 1未发送，2: 已经发送-已响应
     * @var int
     */
    private int $sendGiveUp = 1;

    /**
     * 是否在线
     * @var bool
     */
    private bool $isOnline = true;

    private bool $isMyTurn = false;

    /**
     * 玩家Socket连接器
     * @var SocketHandler
     */
    private SocketHandler $connection;

    /**
     * 初始化玩家
     * @param string $room_id    房间ID
     * @param int $uid          玩家ID
     * @param string $name      玩家名称
     * @param int $homeowner    玩家是否是房主
     */
    public function __construct(string $room_id, int $uid, string $name, int $homeowner)
    {
        $this->room_id = $room_id;
        $this->uid = $uid;
        $this->name = $name;
        $this->homeowner = $homeowner;
        $this->pic = '/src/assets/image/user-avatar.jpg';
        $this->score = 0;   //TODO 使用Redis取值
        $this->chess = 0;
        $this->mover = 0;
    }

    /**
     * 初始化玩家信息
     * @return void
     */
    public function init(){
        $this->isMyTurn = false;
        $this->sendWithdraw = $this->sendGameTie = $this->sendGiveUp = 1;
        $this->notifyManage();
    }

    /**
     * 获取玩家ID
     * @return int
     */
    public function getUid(): int
    {
        return $this->uid;
    }

    /**
     * 判断是否是房主
     * @return bool
     */
    public function isHomeowner(): bool
    {
        return $this->homeowner === 1;
    }

    /**
     * 设置玩家信息
     * @param int $chess    棋子
     * @param int $mover    先后顺序
     * @return void
     */
    public function setPlayerInfo(int $chess, int $mover)
    {
        $this->chess = $chess;
        $this->mover = $mover;
//        $this->isMyTurn = $chess === 1; //白棋先手
        $this->notifyManage();
    }

    /**
     * 更新玩家回合
     * @return void
     */
    public function updateTurn($flag = null){
        $this->isMyTurn = $flag !== null ? $flag : !$this->isMyTurn;
        $this->notifyManage();
    }

    /**
     * 设置玩家Socket连接器
     * @param SocketHandler $connection
     * @return void
     */
    public function setConnection(SocketHandler $connection)
    {
        !empty($this->connection) and $this->connection->close();
        $this->connection = $connection;
        $this->notifyManage();
    }

    /**
     * 获取玩家Socket连接器
     * @return SocketHandler
     */
    public function getConnection(): SocketHandler
    {
        return $this->connection;
    }

    /**
     * 获取发送请求
     * @param int $key
     * @return array
     */
    public function getSendRequest(int $key): array
    {
        $title = '';
        $mode = '';
        switch($key){
            case 1: //悔棋
                $flag = $this->sendWithdraw;
                $title = '悔棋';
                $mode = 'withdraw';
                break;
            case 2: //和棋
                $flag = $this->sendGameTie;
                $title = '和棋';
                $mode = 'gameTie';
                break;
            case 3: //认输
                $flag = $this->sendGiveUp;
                break;
            default:
                $flag = 1;
                break;
        }
        return [$flag, $title, $mode, '一局只有一次发送的机会', '对方请求' . $title . '，你可以选择同意/拒绝'];
    }

    /**
     * 更新发送请求
     * @param int $key
     * @param int $flag
     * @return void
     */
    public function updateSendRequest(int $key, int $flag){
        switch($key){
            case 1: //悔棋
                $this->sendWithdraw = $flag;
                break;
            case 2: //和棋
                $this->sendGameTie = $flag;
                break;
            case 3: //认输
                $this->sendGiveUp = 2;
                break;
        }
        $this->notifyManage();
    }

    /**
     * 获取玩家可发送的数据
     * @param string $key
     * @return array|mixed
     */
    public function getMyCanSendData(string $key = '')
    {
        $arr = [
            'uid' => $this->uid,
            'name' => $this->name,
            'pic' => $this->pic,
            'score' => $this->score,
            'chess' => $this->chess,
            'turn' => $this->isMyTurn,
            'sendWithdraw' => $this->sendWithdraw,
            'sendGameTie' => $this->sendGameTie,
        ];
        return $arr[$key] ?? $arr;
    }

    /**
     * 获取玩家回合
     * @return bool
     */
    public function isMyTurn(): bool
    {
        return $this->isMyTurn;
    }

    /**
     * 上线
     * @return void
     */
    public function online(){
        $this->isOnline = true;
        $this->notifyManage();
    }

    /**
     * 掉线
     * @return void
     */
    public function offline(){
        $this->isOnline = false;
        $this->notifyManage();
    }

    /**
     * 获取玩家是否在线
     * @return bool
     */
    public function getOnlineStatus(): bool
    {
        return $this->isOnline;
    }

}